package v2;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class Particle implements Acceleratable {

	private ParticleManager myManager = null;

	private State2D state;

	private double charge;

	private Acceleration stepAcceleration = new Acceleration();

	/**
	 * Constant applied to acceleration
	 */
	public static final double K = 1.0;

	/**
	 * Constant applied to velocity to create friction
	 */
	public static final double MU = 0.5;

	public Particle(double x, double y) {
		state = new State2D();
		state.setPX(x);
		state.setPY(y);
		charge = 1;
	}

	public Particle(State2D state) {
		this.state = state;
		charge = 1;
	}

	public Particle(double x, double y, double charge) {
		state = new State2D();
		state.setPX(x);
		state.setPY(y);
		this.charge = charge;
	}

	public Particle(State2D state, double charge) {
		state = new State2D();
		this.state = state;
		this.charge = charge;
	}

	public State2D getState() {
		return state;
	}

	public double getCharge() {
		return charge;
	}

	public Point getPoint() {
		return state.getPointPos();
	}

	public Acceleration getAcc() {
		calcAcceleration();
		Acceleration old = stepAcceleration;
		stepAcceleration = new Acceleration();
		return old;
	}

	public void calcAcceleration() {
		ArrayList<Particle> particles = myManager.getParticles();
		for (Particle p : particles) {
			if (p != this) {
				Point otherPosition = p.getPoint();
				Point position = this.getPoint();

				double otherCharge = p.getCharge();

				double distanceSQ = position.distanceSq(otherPosition);
				double angle = position.angle(otherPosition);

				double totalAcc = K * charge * otherCharge / distanceSQ;

				double accX = Math.cos(angle) * totalAcc;
				double accY = Math.sin(angle) * totalAcc;

				stepAcceleration.addAcc(accX, accY);
				p.stepAcceleration.addAcc(-accX, -accY);
			}
		}
	}

	public void createDrawing(Graphics2D g, int x, int y) {
		g.drawRoundRect(x, y, 3, 3, 2, 2);
	}

	public void setParticleManager(ParticleManager p) {
		myManager = p;
	}

}
